
 ; Program to find out the  N th fibonacci number

 stack segment stack
   db 10 dup (0)
 stack Ends

 data segment
   mes1 db 'Enter `N` : $'
   mes2 db 13,10,'The N th fibonacci number is : $'
   f1   db 0
   f2   db 1
 data ends

 code segment
     assume cs : code , ds : data

   begin : mov ax,data
           mov ds,ax       ; Initialise the data segment

           lea dx,mes1
           mov ah,09h      ; Output the 'i/p prompt'
           int 21h

           mov ah,01h
           int 21h         ; Read 'N'
           sub al,'0'
           jz quit

   again : dec al
           jz over

           mov ah,f1
           add ah,f2       ; f2 = f1 + f2
           mov f2,ah

           mov ah,f2
           sub ah,f1       ; f1 = f2 - f1
           mov f1,ah

           jmp again

   over  : mov dl,'$'
           push dx         ; Push '$' as end of stack
                           ; marker on the stack

           mov bl,10
           mov ah,0
           mov al,f2       ; 'f2' contains the desired number

   loop1 : div bl
           push ax         ; Push quotient and remainder on the stack
           mov ah,0
           cmp al,0        ; divide till quotient becomes 0
           jne loop1

           lea dx,mes2
           mov ah,09h      ; Output the 'o/p prompt'
           int 21h

   loop2 : pop dx
           cmp dl,'$'      ; Check if end of stack has reached
           je quit
           mov dl,dh       ; Move the remainder in the register 'dl'
           add dl,'0'
           mov ah,02h      ; Output a digit at a time
           int 21h
           jmp loop2

   quit  : mov ah,4ch
           int 21h         ; Return to DOS

 code ends
   end begin
